package com.study.security.core.validate.code.impl.image.generator;

import com.study.security.core.properties.SecurityProperties;
import com.study.security.core.validate.code.impl.image.entity.ImageCode;
import com.study.security.core.validate.code.entity.ValidateCode;
import com.study.security.core.validate.code.generator.ValidateCodeGenerator;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @author liyongkui
 */
public class ImageValidateCodeGenerator implements ValidateCodeGenerator {


    private SecurityProperties securityProperties;

    @Override
    public ValidateCode generate(ServletWebRequest request) throws Exception {
        int width = ServletRequestUtils.getIntParameter(request.getRequest(),"width",
                securityProperties.getCode().getImage().getWidth());
        int height = ServletRequestUtils.getIntParameter(request.getRequest(),"height",
                securityProperties.getCode().getImage().getHeight());
        GifCaptcha gifCaptcha = new GifCaptcha(width,height,
                securityProperties.getCode().getImage().getLength());
        gifCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        gifCaptcha.setFont(Captcha.FONT_1);
        ImageCode imageCode = new ImageCode(gifCaptcha.text().toLowerCase(), securityProperties.getCode().getImage().getExpireIn(), gifCaptcha);
        return imageCode;
    }

    public SecurityProperties getSecurityProperties() {
        return securityProperties;
    }

    public void setSecurityProperties(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }
}
